package tingshu.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import tingshu.service.AlbumService;
import tingshu.util.Log;
import tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/studio/album/bind.json")
public class BindJsonServlet extends HttpServlet {
    private final AlbumService albumService = new AlbumService();
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        HttpSession session = req.getSession(false);
        UserVO userVO = null;
        if (session != null) {
            userVO = (UserVO) session.getAttribute("currentUser");
        }

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        if (userVO == null) {
            Log.println("用户未登陆");
            writer.println("用户未登陆");
            return;
        }

        int aid = Integer.parseInt(req.getParameter("aid"));
        Object view = albumService.getBindInfo(userVO,aid);
        String json = objectMapper.writeValueAsString(view);

        writer.println(json);
    }
}
